package cn.jobstart.controller;

import cn.jobstart.dto.PermissionGroupDTO;
import cn.jobstart.dto.PermissionGroupPageDTO;
import cn.jobstart.export.PermissionGroupDomainExport;
import cn.jobstart.modelExt.PermissionGroupExt;
import cn.jobstart.pubInter.IBaseService;
import cn.jobstart.pubInter.exception.SkException;
import cn.jobstart.searchExt.PermissionGroupSearchExt;
import cn.jobstart.service.IGroupRightsService;
import cn.jobstart.service.IPermissionGroupService;
import cn.jobstart.vo.PermissionGroupDetailDomainVO;
import cn.jobstart.vo.PermissionGroupDetailVO;
import cn.jobstart.vo.PermissionGroupDomainVO;
import cn.jobstart.vo.PermissionGroupListVO;
import cn.jobstart.web.common.SkJsonResult;
import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.Set;

/**
 * @author sunke
 * @Date 2023-09-06 15:00:20
 * @description PermissionGroupController      权限组
 */
@Api(tags = "人才管理系统-权限组相关", description = "权限组相关接口")
@RestController(value = "permissionGroupRestfulController")
@RequestMapping("/permissionGroup/")
@Validated
public class PermissionGroupController extends ApiManageController<PermissionGroupDTO, PermissionGroupPageDTO, PermissionGroupExt, PermissionGroupSearchExt, PermissionGroupDomainVO, PermissionGroupDetailDomainVO, PermissionGroupDetailVO, PermissionGroupListVO, String> {

    @Resource(name = "permissionGroupService")
    private IPermissionGroupService permissionGroupService;
    /**
     * 权限组权限关系表
     */
    @Resource
    private IGroupRightsService groupRightsService;


    @Override
    public IBaseService<PermissionGroupExt, PermissionGroupSearchExt, String> getBaseService() throws SkException {
        return permissionGroupService;
    }


    @Override
    public void afterPostAdd(PermissionGroupDTO dto, PermissionGroupExt domain, PermissionGroupDetailVO permissionGroupDetailVO, HttpServletRequest request, HttpSession session) throws SkException {
        super.afterPostAdd(dto, domain, permissionGroupDetailVO, request, session);
        groupRightsService.insertByGroupIdAndRightsIds(domain.getId(), dto.getRightsIds(), false);
    }

    @Override
    public void afterPostMod(PermissionGroupDTO dto, PermissionGroupExt domain, SkJsonResult<PermissionGroupDetailVO> jsonResult, HttpServletRequest request, HttpSession session) throws SkException {
        super.afterPostMod(dto, domain, jsonResult, request, session);
        //添加权限组和权限的关系
        groupRightsService.insertByGroupIdAndRightsIds(domain.getId(), dto.getRightsIds(), true);
    }

    @Override
    public String getBaseRoute() {
        return "/permissionGroup/";
    }

    /**
     * 当前模板名称
     *
     * @return
     */
    @Override
    public String getModelName() {
        return "权限组";
    }

    @Override
    public Class exportExcelClass() {
        return PermissionGroupDomainExport.class;
    }

    @Override
    public Set<String> totalMapSet() {
        Set totalMapSet = new HashSet();
        totalMapSet.add("count(permission_group.pg_id) as countNum");
        return totalMapSet;
    }


}